﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SelfMadeMillionaire.Core.Entities;

namespace SelfMadeMillionaire.Web.Infrastructure.ViewModels.Account
{
    public class PortfoliosViewModel
    {
        public List<UserPortfolio> Portfolios { get; set; }

        public int SelectedPortfolioId { get; set; }
        public string SelectedPortfolioName { get; set; }

        [DisplayName("Symbol")]
        public string NameCompany { get; set; }
        public List<CompanyInfo> InfoList { get; set; }
        public string SymbolsList { get; set; }

        public bool NotFoundSymbol { get; set; }

        public string SortOrderSymbolOptionsHystory { get; set; }

        [DisplayName("Days in Trade")]
        public int DaysInTradeFilter { get; set; }
        [DisplayName("Min Discount to the market")]
        public double MinDiscountFilter { get; set; }
        [DisplayName("Min Annual Return")]
        public string MinAnnualReturnFieldFilter { get; set; }
        public double MinAnnualReturnFilter { get; set; }
        public SelectList MinAnnualReturnFieldList 
        {
            get
            {
                List<SelectListItem> retList = new List<SelectListItem>();
                retList.Add(new SelectListItem() { Text = "Bid", Value = "Bid"});
                retList.Add(new SelectListItem() { Text = "Ask", Value = "Ask"});
                retList.Add(new SelectListItem() { Text = "(Bid+Ask)/2", Value = "(Bid+Ask)/2"});
                retList.Add(new SelectListItem() { Text = "Last", Value = "Last"});

                return new SelectList(retList, "Value", "Text");
            }
        }
        [DisplayName("Min Probability")]
        public double MinProbabilityFilter { get; set; }
        public bool IsCallsFilter { get; set; }

        public IEnumerable<SymbolOptionsHistory> Hystory { get; set; }
        public List<SymbolOptionsHistory> FilteredHystory
        {
            get
            {
                List<SymbolOptionsHistory> result = new List<SymbolOptionsHistory>();
                if (Hystory != null)
                {
                    var filtered = Hystory.Where(x => x.DaysInTrade > DaysInTradeFilter);
                    filtered = filtered.Where(x => x.GetDiscountWhole() > MinDiscountFilter);
                    if (MinAnnualReturnFieldFilter == "Bid")
                        filtered = filtered.Where(x => x.BidAnnualReturn >= MinAnnualReturnFilter);
                    else if (MinAnnualReturnFieldFilter == "Ask")
                        filtered = filtered.Where(x => x.AskAnnualReturn >= MinAnnualReturnFilter);
                    else if (MinAnnualReturnFieldFilter == "(Bid+Ask)/2")
                        filtered = filtered.Where(x => x.GetBidAsk2Whole() >= MinAnnualReturnFilter);
                    else if (MinAnnualReturnFieldFilter == "Last")
                        filtered = filtered.Where(x => x.LastAnnualReturn >= MinAnnualReturnFilter);
                    filtered = filtered.Where(x => x.Probability >= MinProbabilityFilter);
                    filtered = filtered.OrderBy(x => x.Symbol).ThenBy(x => x.AddDateTime);

                    return filtered.ToList();
                }
                else
                    return result;
            }
        }

        public PortfoliosViewModel()
        {
            InfoList = new List<CompanyInfo>();

            Hystory = new List<SymbolOptionsHistory>();
            DaysInTradeFilter = 0;
            MinDiscountFilter = 0;
            MinAnnualReturnFieldFilter = "(Bid+Ask)/2";
            MinAnnualReturnFilter = 0;
            MinProbabilityFilter = 0;
            IsCallsFilter = true;
        }
    }

    public class PortfolioDialogModel
    {
        public int Id { get; set; }
        [DisplayName("Watch List Name")]
        [Required(ErrorMessage = "Name is required")]
        public string Name { get; set; }
    }

    public class CompanyInfo
    {
        public int PortfolioId { get; set; }
        public string Symbol { get; set; }
        public string Url { get; set; }

        public string Price { get; set; }
        public string PrevClose { get; set; }
        public string Open { get; set; }
        public string Bid { get; set; }
        public string Ask { get; set; }
        public string TargetEst { get; set; }
        public string Beta { get; set; }
        public string NextEarningsDate { get; set; }

        public string DaysRange { get; set; }
        public string Wk52Range { get; set; }
        public string Volume { get; set; }
        public string AvgVol3m { get; set; }
        public string MarketCap { get; set; }
        public string PE { get; set; }
        public string EPS { get; set; }
        public string Div { get; set; }
    }
}